/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Game.bc;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;

/**
 *
 * @author ashish.vaidya
 */
@Singleton
public class GameController implements GameControllerLocal {

    private HashMap<Player, Game> games = new HashMap<Player, Game>();
    private int GID = 0;
    private Player defaultPlayer = new Player("Default");
    private Game dg = new Game(defaultPlayer, 4, GID);
    //to be removed later
   /* @PersistenceContext(unitName="EntityBean")
    EntityManager em;
    protected PlayerEntity player;
     */

    @PostConstruct
    public void init() {
        System.out.println("***Controller Initialized.***");
        games.put(defaultPlayer, dg);
    }

    public Game createGame(Player user, int wordLength) {
        /*  EntityManagerFactory emf = Persistence.createEntityManagerFactory("bullsandcows-PU");
        EntityManager em = emf.createEntityManager();
        Hand h = new Hand();
        h.setId(Short.MIN_VALUE);
        em.getTransaction().begin();
        em.persist(h);
        em.getTransaction().commit();
        em.close();
         */ boolean gameExists = false;
        Game g = null;
        Iterator iterator = games.keySet().iterator();
        while (iterator.hasNext()) {
            Player key = ((Player) iterator.next());
            if (key.getName().equals(user.getName())) {
                gameExists = true;
                g = games.get(key);
            }

        }

        if (!gameExists) {
            System.out.println("***Game Created*** " + wordLength + " BY : " + user.getName());
            GID++;
            g = new Game(user, wordLength, GID);
            games.put(user, g);
            return g;
        } else {
            System.out.println("***Game Created Returning Same old game***" + wordLength);
            g.reinit(wordLength);
            return g;
        }
    }

    public Game joinGame(String playerName) {
        System.out.println("Joining game:" + playerName);
        Set set = games.entrySet();
        Iterator i = set.iterator();
        Game game = null;
        Player player = null;
        while (i.hasNext()) {

            Map.Entry me = (Map.Entry) i.next();
            player = ((Player) me.getKey());
            if (playerName.equals(player.getName())) {
                game = games.get(player);
            }
            System.out.println("Game:" + player.getName());
            // System.out.println(me.getKey() + " : " + me.getValue() );
        }
        if (game != null) {
            game.joinGame(player);
        }
        return game;
    }

    public HashMap<Player, Game> getGames() {
        return games;
    }

    /*  public void registerPlayer(String userName, String passWord) {
    if (player == null)
    player = new PlayerEntity(userName, passWord);
    em.persist(player);
    }
     */
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
